<template>
    <el-breadcrumb class="app-breadcrumb" separator="/">
        <transition-group name="breadcrumb">
            <el-breadcrumb-item
                v-for="(item, index) in levelList"
                :key="item.path"
            >
                <span
                    v-if="
                        item.redirect === 'noRedirect' ||
                        index == levelList.length - 1
                    "
                    class="no-redirect"
                    >{{ item.meta.title }}</span
                >
                <a v-else @click.prevent="handleLink(item)">{{
                    item.meta.title
                }}</a>
            </el-breadcrumb-item>
        </transition-group>
    </el-breadcrumb>
</template>

<script>
export default {
    data() {
        return {
            levelList: null,
        };
    },
    watch: {
        $route(route) {
            // if you go to the redirect page, do not update the breadcrumbs
            if (route.path.startsWith("/redirect/")) {
                return;
            }
            this.getBreadcrumb();
        },
    },
    created() {
        this.getBreadcrumb();
    },
    methods: {
        getBreadcrumb() {
            // only show routes with meta.title
            let matched = this.$route.matched.filter(
                (item) => item.meta && item.meta.title
            );
            const first = matched[0];

            if (!this.isDashboard(first)) {
                matched = [{ path: "/", meta: { title: "首页" } }].concat(
                    matched
                );
            }

            this.levelList = matched.filter(
                (item) =>
                    item.meta &&
                    item.meta.title &&
                    item.meta.breadcrumb !== false
            );

            if (
                this.$store.getters.allDefectObj["HR-WEB-079"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "请假扣费规则管理"
            ) {
                this.levelList[this.levelList.length - 1].meta.title =
                    "请假规则";
            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-071"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "加班登记"
            ) {
                this.levelList[this.levelList.length - 1].meta.title =
                    "加班管理";
            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-103"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "已终止合同"
            ) {
                //删除倒数数组倒是第二个
                this.levelList.splice(this.levelList.length - 2, 1);
          
            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-175"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "个人惩罚信息"
            ) {
                //删除倒数数组倒是第二个
                this.levelList.splice(this.levelList.length - 2, 1);
          
            }



        },
        isDashboard(route) {
            const name = route && route.name;
            if (!name) {
                return false;
            }
            return name.trim() === "Index";
        },
        handleLink(item) {
            if (
                this.$store.getters.allDefectObj["HR-WEB-006"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "系统用户管理"
            ) {
                return;
            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-076"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "加班计费规则管理"
            ) {
                return;
            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-160"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "劳动合同"
            ) {
                this.$router.push("/commonWorker/personInfoManager");
                return;

            }
            if (
                this.$store.getters.allDefectObj["HR-WEB-180"] == 0 &&
                this.levelList[this.levelList.length - 1].meta.title ==
                    "个人考评信息"
            ) {
                this.$router.push("/commonWorker/personInfoManager");
                return;

            }

            const { redirect, path } = item;
            if (redirect) {
                this.$router.push(redirect);
                return;
            }
            this.$router.push(path);
        },
    },
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 8px;

    .no-redirect {
        color: #97a8be;
        cursor: text;
    }
}
</style>
